/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vadung.contragame2d;

import javax.microedition.lcdui.Graphics;

/**
 *
 * @author dung.voanh
 */
public class MyEnemy extends MyActor{
    
    private int m_iJumpHeight;
    private boolean m_isJump;
    private boolean m_isFall;
    private boolean m_isGoback;
    private int m_iYJumpMax;
    private int m_iDetalX;
    private int m_iDetalY;
    private int m_iMaxBullet; // Max bullet shot once time    
    private int m_iKeyRandom; // Use to update enemy's state
    

    public MyEnemy(int[] aiSpriteID) {
        super(aiSpriteID);
        Init();
    }

    public MyEnemy(String[] astrSpriteName) {
        super(astrSpriteName);
        Init();
    }

    private void Init() {
        m_isJump = false;
        m_isFall = true;
        m_isGoback = false;
        m_iJumpHeight = 60;
        m_iYJumpMax = m_iY - m_iJumpHeight;
        m_iDetalX = 4;
        m_iDetalY = 6;
        m_isDie = false;
        m_iMaxBullet = 2;
        m_iKeyRandom = 0;
    }

    private void AddKeyAction(int keyGame) {
        m_iKeyRandom |= keyGame;
    }

    private void RemoveKeyAction(int keyGame) {
        m_iKeyRandom &= ~keyGame;
    }

    private void RandomKeyAction(){
        int value = DEFINE.RAND.nextInt(6);
        //int key = Math.pow(2, value);    
        int keyGame;
        switch(value){
            case 0: keyGame = DEFINE.KEY_UP;
                break;  
                
            case 1: keyGame =  DEFINE.KEY_DOWN;
                break;    
                
            case 2:keyGame =  DEFINE.KEY_LEFT;
                break;   
                
            case 3:keyGame =  DEFINE.KEY_RIGHT;
                break;     
                
            case 4:keyGame =  DEFINE.KEY_FIRE;
                break;    
                
            default:keyGame =  DEFINE.KEY_SOMETHING;
                break;
        }
        if((m_iKeyRandom & keyGame) == keyGame){
            RemoveKeyAction(keyGame);
        }else{
            AddKeyAction(keyGame);
        }
    }
    
    public void Render(Graphics g) {
        super.Render(g); //To change body of generated methods, choose Tools | Templates.
        if (m_isDie) {
        }
    }

    public void Update() {
        //Update position in display
        super.Update();
         
        if (m_isDie) {
            return;
        }
        
        MySprite sprEnemy = this.GetCurrentSpriteAction();
        GameLayerManager layerManager = GameLayerManager.GetInstance();
        int key = m_iKeyRandom;
        
        if(((key & DEFINE.KEY_RIGHT) == DEFINE.KEY_RIGHT) &&  ((key & DEFINE.KEY_LEFT) == DEFINE.KEY_LEFT)){
           m_iKeyRandom = key = 0;
        }
        
        if(m_iKeyRandom == 0 || m_iKeyRandom == DEFINE.KEY_SOMETHING){
            RandomKeyAction();
        }

        //System.out.println("Key action: " + key);
        // Ênmy fall endless...
        if (layerManager.IsOutMap(m_iXMap, m_iYMap + m_iHeight)) {
            System.out.println("Enemy is died.");
            m_isDie = true;
        }

        if (m_isJump) {
            this.MoveOnMap(0, -m_iDetalY);
            if (m_iY <= m_iYJumpMax) {
                m_isJump = false;
                m_isFall = true;
            }
        }
        if (m_isFall) {
            this.MoveOnMap(0, m_iDetalY);
            // When player collision obstacle, stop fall
            boolean isCollision = layerManager.CheckCollision(1, m_iXMap + 5, m_iYMap, m_iWidth - 10, m_iHeight);
            if (isCollision) {
                m_isFall = false;
                RandomKeyAction();
                boolean isCollisionAtop = layerManager.CheckCollision(1, m_iXMap + m_iWidth / 2, m_iYMap + layerManager.GetTiledHeight());
                if (isCollisionAtop) {
                    m_isFall = true;
                }
            }
        }
        
        m_state = DEFINE.ACTOR_STATE_NORMAL;
        if (key != 0) {
            //---------------------------------MOVE LEFT-------------------------------------- 
            if ((key & DEFINE.KEY_LEFT) == DEFINE.KEY_LEFT) {
                m_state = DEFINE.ACTOR_STATE_MOVE_LEFT;
                m_isGoback = true;

                if (m_iXMap > 0 && layerManager.CheckCollision(1, m_iXMap, m_iYMap, 0, m_iHeight - layerManager.GetTiledHeight())) {
                    RemoveKeyAction(DEFINE.KEY_LEFT);
                    RandomKeyAction();
                } else {
                    this.MoveOnMap(-m_iDetalX, 0); 
                }
                
                if (sprEnemy.GetCurrentFrame() > sprEnemy.GetTotalFrame()/2 - 1) {
                    sprEnemy.SetCurrentFrame(sprEnemy.GetTotalFrame()/2 - 1);
                } else {
                    sprEnemy.PreviousFrame();
                }
                
                //Fall when local bottom is empty
                if (m_isFall == false && m_isJump == false) {
                    m_isFall = !layerManager.CheckCollision(1, m_iXMap, m_iYMap, m_iWidth - 10, m_iHeight);
                }
            }
            ////---------------------------------MOVE RIGHT---------------------------------
            if ((key & DEFINE.KEY_RIGHT) == DEFINE.KEY_RIGHT) {
                m_state = DEFINE.ACTOR_STATE_MOVE_RIGHT;
                m_isGoback = false;
                if (layerManager.CheckCollision(1, m_iXMap + m_iDetalX, m_iYMap, m_iWidth, m_iHeight - layerManager.GetTiledHeight())) {
                    RemoveKeyAction(DEFINE.KEY_RIGHT);
                    RandomKeyAction();
                } else {
                    this.MoveOnMap(m_iDetalX, 0); 
                }
                
                if (sprEnemy.GetCurrentFrame() <= sprEnemy.GetTotalFrame()/2) {
                    sprEnemy.SetCurrentFrame(sprEnemy.GetTotalFrame()-1);
                } else {
                    sprEnemy.PreviousFrame();
                }

                //Fall when local bottom is empty
                if (m_isFall == false && m_isJump == false) {
                    m_isFall = !layerManager.CheckCollision(1, m_iXMap + 5, m_iYMap, m_iWidth - 12, m_iHeight);
                }
            }

            ////---------------------------------UP---------------------------------
            if ((key & DEFINE.KEY_UP) == DEFINE.KEY_UP) {
                m_state = DEFINE.ACTOR_STATE_UP;
                if (m_isFall == false && m_isJump == false) {
                    m_iYJumpMax = m_iY - m_iJumpHeight;
                    m_isJump = true;
                }
                RemoveKeyAction(DEFINE.KEY_UP);
            }

            ////---------------------------------DOWN---------------------------------
            if ((key & DEFINE.KEY_DOWN) == DEFINE.KEY_DOWN) {
                m_state = DEFINE.ACTOR_STATE_DOWN;
                if (m_isFall == false && m_isJump == false) {
                    boolean isCollision = layerManager.CheckCollision(1, m_iXMap, m_iYMap + layerManager.GetTiledHeight(), m_iWidth, m_iHeight);
                    if (isCollision == false) {
                        this.MoveOnMap(0, layerManager.GetTiledHeight());
                        m_isJump = false;
                        m_isFall = true;
                    }
                }
                RemoveKeyAction(DEFINE.KEY_DOWN);
            }

            ////---------------------------------FIRE---------------------------------
            if ((key & DEFINE.KEY_FIRE) == DEFINE.KEY_FIRE) {
                if (m_iMaxBullet > MyObject.GetInstance().GetSumBullet(this) && layerManager.IsOutWindowView(m_iXMap, m_iYMap, m_iWidth, m_iHeight) == false) {
                    int aiSpriteID[] = new int[]{2};
                    MyBullet bullet = new MyBullet(this, aiSpriteID);
                    bullet.SetPositionOnMap(m_iXMap + m_iWidth / 2, m_iYMap + m_iHeight / 4);
                    //Set direction fly for bullet
                    if (m_isGoback) {
                        bullet.m_state = DEFINE.ACTOR_STATE_MOVE_LEFT;
                    }

                    MyObject.GetInstance().Add(bullet);

                }
                   this.RemoveKeyAction(DEFINE.KEY_FIRE);
            }

        } else {
            if (m_isGoback) {
                sprEnemy.SetCurrentFrame(sprEnemy.GetTotalFrame()/2 - 1);
            } else {
                sprEnemy.SetCurrentFrame(sprEnemy.GetTotalFrame()/2);
            }
        }
    }
}
